﻿using System.Web.Mvc;
using System.Web.UI;
using System.Web.Routing;
using Wwtfly.BBS.Framework.Common;
using Wwtfly.BBS.BackModels.ViewModels;
using Wwtfly.Utility.WebUtility;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Web.Mvc.Html;
using Wwtfly.BBS.BackModels.Models.Users.BBSPortalIdentity;

namespace Wwtfly.Extensions
{
    /// <summary>
    /// 按钮
    /// </summary>
    public static class HtmlMyExtension
    {

        #region 按钮相关
        /// <summary>
        /// 提交 按钮
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="value">值</param>
        /// <param name="id">ID值</param>
        /// <param name="htmlAttributes">html属性</param>
        /// <returns></returns>
        public static MvcHtmlString Submit(this HtmlHelper helper, string value, string id = null, object htmlAttributes = null)
        {
            return Button(helper, "submit", value, id, htmlAttributes);
        }


        /// <summary>
        /// 按钮
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="value">值</param>
        /// <param name="id">ID值</param>
        /// <param name="htmlAttributes">html属性</param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper helper, string value, string id = null, object htmlAttributes = null)
        {
            return Button(helper, "button", value, id, htmlAttributes);
        }

        /// <summary>
        /// 按钮
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="type">类型</param>
        /// <param name="value">值</param>
        /// <param name="id">ID值</param>
        /// <param name="htmlAttributes">html属性</param>
        /// <returns></returns>
        private static MvcHtmlString Button(this HtmlHelper helper, string type, string value, string id = null, object htmlAttributes = null)
        {
            TagBuilder tagBuilder = new TagBuilder("input");
            tagBuilder.MergeAttribute("type", type);
            if (!string.IsNullOrEmpty(value))
            {
                tagBuilder.MergeAttribute("value", value);
            }
            if (!string.IsNullOrEmpty(id))
            {
                tagBuilder.MergeAttribute("id", id);
            }
            if (htmlAttributes != null)
            {
                tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            }
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }

        #endregion


        #region Ohter


        /// <summary>
        /// 创建分页
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="pageParams">分页参数</param>
        public static string Pager(this HtmlHelper helper, UrlHelper urlHelper, string actionName = "Index",
            PageParams pageParams = null)
        {
            BaseViewModel model = helper.ViewData.Model as BaseViewModel;
            pageParams = pageParams == null
                ? (model != null ? model.PageParams : new PageParams())
                : new PageParams();
            pageParams = pageParams ?? new PageParams();
            Dictionary<string, string> dic =
                PageHelper.GetPageLanguage("首页", "上一页", "下一页", "尾页", "共<b>{0}</b>条记录&nbsp;", "GO");

            return new PageHelper(pageParams, dic).CreatePage(urlHelper, actionName);
        }


        /// <summary>
        /// 获取 编辑的 ActionName
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetEditActionName(this HtmlHelper helper, int id)
        {
            return id > 0 ? "Update" : "Add";
        }

        /// <summary>
        /// 输出 title
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <returns>string</returns>
        public static string GetTitle(this HtmlHelper helper)
        {
            return string.Format("{0}", (helper.ViewData["Title"] ?? "BBS"));
        }


        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static BBSPortalIdentity GetUser(this HtmlHelper helper)
        {
            return helper.ViewContext.HttpContext.User as BBSPortalIdentity;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="htmlAttribues">html 属性</param>
        /// <summary>
        /// 返回包含"是"和"否"项的下拉列表
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="expression"></param>
        /// <param name="optionLabel">默认空项的文本。此参数可以为 null。</param>
        /// <param name="htmlAttributes">属性</param>
        /// <returns></returns>
        public static MvcHtmlString DropDownListYesOrNoFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
            Expression<Func<TModel, TProperty>> expression,
            string optionLabel = null, object htmlAttributes = null)
        {
            List<SelectListItem> valueList = new List<SelectListItem>() {
                new SelectListItem() { Text ="是", Value = "true" },
                new SelectListItem() { Text = "否", Value = "false" }
            };
            ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            SelectList sl = new SelectList(valueList, "Value", "Text", metaData.Model);
            return helper.DropDownListFor(expression, sl, optionLabel, htmlAttributes);
        }


        /// <summary>
        /// 将 form 开始标记写入响应。在用户提交窗体时，将由某个操作方法处理该请求。
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName)
        {
            return htmlHelper.BeginForm(actionName,
                Convert.ToString(htmlHelper.ViewContext.RouteData.Values["controller"]));
        }

        #endregion
    }
}
